package com.dondedesigns.eh.controller.profile;

import org.apache.struts2.interceptor.validation.SkipValidation;

import com.dondedesigns.eh.controller.index.UserAware;
import com.dondedesigns.eh.persistence.User;
import com.dondedesigns.eh.persistence.dao.UserDAO;
import com.dondedesigns.eh.util.MessageConstants;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.validator.annotations.Validations;

public class Profile extends ActionSupport implements UserAware, ModelDriven<User> {
	
	private User user;
	private UserDAO userDAO;
	
	@Override
	@SkipValidation
	public String input() throws Exception {
		return SUCCESS;
	}
	
	@Override
	public String execute() throws Exception {
		//userDAO.update(user);
		return SUCCESS;
	}

	@Override
	public void setUser(User user) {
		this.user = user;
	}
	
	@Override
	public User getModel() {
		return user;
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
	@Override
	public void validate() {
		User tempUser;
		/*if (user.getEmail().equals("")) {
			addActionError(getText(MessageConstants.EMAIL_REQUIRED));
		} else if (!user.getEmail().matches("[\\w\\-\\.]+@[\\w\\-]+\\.[\\w]+[\\.]?+[\\w]*")) {
			addActionError(getText(MessageConstants.EMAIL_ERROR));
		} else if ((tempUser = userDAO.getUserByEmail(user)) != null && !user.equals(tempUser)) {
			addActionError(getText(MessageConstants.EMAIL_EXISTS));
		}*/
		//TODO: Check if the username/email was changed
		if (user.getName().equals("")) {
			addActionError(getText(MessageConstants.NAME_REQUIRED));
		}
		if (!(user.getPassword().equals("") && user.getConfirmPassword().equals(""))) {
			if (!user.getPassword().equals(user.getConfirmPassword())) {
				addActionError(getText(MessageConstants.PASSWORD_MISMATCH));
			}
		}
	}
}
